/*
 * @Author: CSG@HYC
 * @Date: 2025-07-14 10:17:28
 * @FilePath: \MDK-ARMe:\Desktop\F1_template\HW\Src\uart.c
 */

 #include "uart.h"

 UART_HandleTypeDef huart1;


 void USART1_Init(uint32_t baudrate)
 {
 	huart1.Instance = USART1;
 	huart1.Init.BaudRate = baudrate;
 	huart1.Init.WordLength = UART_WORDLENGTH_8B;
 	huart1.Init.StopBits = UART_STOPBITS_1;
 	huart1.Init.Parity = UART_PARITY_NONE;
 	huart1.Init.Mode = UART_MODE_TX_RX;
 	huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 	huart1.Init.OverSampling = UART_OVERSAMPLING_16;	// 16倍采样技术

 	if (HAL_UART_Init(&huart1) != HAL_OK)
 	{
 		// Error_Handler();
 	}
	
 }

 // 底层GPIO初始化回调函数，不需要自己调用，在HAL_UART_Init函数中自动调用
 void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
 {
 	GPIO_InitTypeDef GPIO_InitStruct = {0};

 	// 对于HAL库底层的回调函数，在调用时都需要对传入的参数进行判断来确认是哪一个硬件需要进行操作
 	if (uartHandle->Instance == USART1)
 	{
 		/**
 		 * INIT USART1
 		 * TX PA9	MODE AF_PP
 		 * RX PA10	MODE AF_PP
 		 */

 		// Enable Clock

 		__HAL_RCC_USART1_CLK_ENABLE();
 		__HAL_RCC_GPIOA_CLK_ENABLE();

 		// Init Pins
 		GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
 		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 		GPIO_InitStruct.Pull = GPIO_NOPULL;
 		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 	}
	
 }

 int fputc(int ch, FILE *file)
 {
 	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1000);
 	return ch;
 }

